package server;

import java.io.IOException;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;

import client.Authenticator;

public class ServerAuthenticator extends Authenticator{
	private SSLServerSocket serverSocket;
	
	public ServerAuthenticator(String password, String keyStorePath,
			String trustStorePath, int port) {
        SSLServerSocketFactory serverSocketFactory = null;
		try {
            SSLContext context = getContext(password, keyStorePath, trustStorePath);
            serverSocketFactory = context.getServerSocketFactory();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
			serverSocket = (SSLServerSocket) serverSocketFactory.createServerSocket(port);
	        serverSocket.setNeedClientAuth(true);
		} catch (IOException e) {
            System.out.println("Unable to start server: " + e.getMessage());
            e.printStackTrace();
		}
	}

	public SSLSocket validateIncomingConnection() {
        SSLSocket socket;
        try {
        	socket = (SSLSocket)serverSocket.accept();
        	return socket;
        } catch (IOException e) {
            System.out.println("Server died: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
	}
}
